package com.yujing.yview

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.yujing.yview.base.YBaseDialog
import com.yujing.yview.databinding.DialogWebviewBinding

/**
 * WebView对话框
 *
 * @author yujing 2019年12月17日14:04:33
 */
/*
用法
val dialog = WebViewDialog(this,"http://www.baidu.com",false)
dialog.show()
 */
class WebViewDialog @JvmOverloads constructor(activity: Activity, url: String, isCancelable: Boolean = true) : YBaseDialog<DialogWebviewBinding>(activity, R.layout.dialog_webview, android.R.style.Theme_DeviceDefault_Dialog_NoActionBar) {
    private val url: String

    //初始化完成
    var initCompleteListener: ((DialogWebviewBinding) -> Unit)? = null

    init {
        cancel = isCancelable
        this.url = url
        widthPixels = 0.95f
        heightPixels = 0.95f
        fillColor = Color.parseColor("#EEFFFFFF")
    }

    override fun init() {
        binding.webView.setBackgroundColor(Color.parseColor("#00000000"))
        binding.ivExit.setOnClickListener { v -> dismiss() }
        //ivExit.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(activity, R.color.red), PorterDuff.Mode.SRC_ATOP));
        setSettings(binding.webView)
        setClient(binding.webView)
        binding.webView.loadUrl(url)
        initCompleteListener?.invoke(binding)
    }

    override fun dismiss() {
        super.dismiss()
        binding.webView.destroy()
    }

    companion object {
        /**
         * 设置WebView跳转拦截
         *
         * @param webView webView
         */
        fun setClient(webView: WebView) {
            webView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
                    if (Build.VERSION.SDK_INT >= 21) {
                        val url = request.url.toString()
                        if (url.startsWith("mailto:") || url.startsWith("geo:") || url.startsWith("tel:") || url.startsWith("smsto:")) {
                            //Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
                            //startActivity(intent);
                            return true
                        }
                        view.loadUrl(url)
                    }
                    // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                    return true
                }
            }
        }

        /**
         * 设置WebView
         *
         * @param webView webView
         */
        @SuppressLint("SetJavaScriptEnabled")
        fun setSettings(webView: WebView) {
            val webSettings = webView.settings
            webSettings.defaultTextEncodingName = "utf-8"
            webSettings.javaScriptEnabled = true // 设置WebView属性，能够执行Javascript脚本
            webSettings.javaScriptCanOpenWindowsAutomatically = true
            webSettings.allowFileAccess = true // 设置可以访问文件
            webSettings.builtInZoomControls = true // 设置支持缩放
            webSettings.displayZoomControls = false // 设定缩放控件隐藏
            webSettings.loadsImagesAutomatically = true // 支持自动加载图片
            webSettings.domStorageEnabled = true
            webSettings.databaseEnabled = true
            webSettings.setSupportZoom(true)
            webView.isVerticalScrollBarEnabled = false
            webView.isHorizontalScrollBarEnabled = false
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            //        webSettings.setAppCacheEnabled(false);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW //允许其加载混合网络协议内容即可,https和http混合页面
            }
        }
    }
}